//
//  GuidePageView.m
//  WindRunner
//
//  Created by leeqb on 3/9/15.
//  Copyright (c) 2015 poweropt. All rights reserved.
//

#import "GuidePageView.h"

@implementation GuidePageView

+ (void)showGuidePage
{
    NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    BOOL isGuidePageShow = [userDefaults boolForKey:[NSString stringWithFormat:@"IsGuidePageShow_%@", appVersion]];
    if(!isGuidePageShow) {
        CGRect rect = CGRectZero;
        rect.size.width = SCREEN_WIDTH;
        rect.size.height = SCREEN_HEIGHT;
        GuidePageView *guidPage = [[GuidePageView alloc] initWithFrame:rect];
        guidPage.backgroundColor = RGB(114, 194, 255);
        [[[UIApplication sharedApplication] keyWindow] addSubview:guidPage];
        
        [userDefaults setBool:YES forKey:[NSString stringWithFormat:@"IsGuidePageShow_%@", appVersion]];
        [userDefaults synchronize];
    }
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self) {
        _animDuration = 1;
        
        UIImage *personImage = [UIImage imageNamed:@"guide_person.png"];
        UIImage *wordImage = [UIImage imageNamed:@"guide_word.png"];
        UIImage *startImage = [UIImage imageNamed:LocalStringBase(@"GUIDE_START_BUTTON_IMAGE")];
        UIImage *cloudImage = [UIImage imageNamed:@"guid_earth_cloud.png"];
        
        CGRect rect = CGRectZero;
        rect.size.width = personImage.size.width / 2;
        rect.size.height = personImage.size.height / 2;
        rect.origin.x = (SCREEN_WIDTH - rect.size.width) / 2;
        rect.origin.y = 40;
        _personImageView = [[UIImageView alloc] initWithFrame:rect];
        _personImageView.image = personImage;
        [self addSubview:_personImageView];
        
        rect.size.width = cloudImage.size.width / 2.5;
        rect.size.height = cloudImage.size.height / 2.5;
        rect.origin.x = -215;
        rect.origin.y = 110;
        _cloudImageView = [[UIImageView alloc] initWithFrame:rect];
        _cloudImageView.image = cloudImage;
        [self addSubview:_cloudImageView];
        
        rect.size.width = wordImage.size.width / 2.2;
        rect.size.height = wordImage.size.height / 2.2;
        rect.origin.x = (SCREEN_WIDTH - rect.size.width) / 2;
        rect.origin.y = _personImageView.frame.origin.y + _personImageView.frame.size.height + 45;
        _wordImageView = [[UIImageView alloc] initWithFrame:rect];
        _wordImageView.image = wordImage;
        [self addSubview:_wordImageView];
        
        rect.size.width = startImage.size.width / 2.2;
        rect.size.height = startImage.size.height / 2.2;
        rect.origin.x = (SCREEN_WIDTH - rect.size.width) / 2;
        rect.origin.y = _wordImageView.frame.origin.y + ((_wordImageView.frame.size.height - rect.size.height)) / 2;
        _startButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _startButton.frame = rect;
        _startButton.alpha = 0;
        [_startButton setImage:startImage forState:UIControlStateNormal];
        [_startButton addTarget:self action:@selector(startButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_startButton];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandle:)];
        [self addGestureRecognizer:tapGesture];
    }
    return self;
}

- (void)tapHandle:(UITapGestureRecognizer *)tapGesture
{
    if(!_isAnimated) {
        _isAnimated = YES;
        CABasicAnimation *personAnim = [CABasicAnimation animationWithKeyPath:@"position.y"];
        personAnim.duration = _animDuration;
        personAnim.autoreverses = YES;
        personAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        personAnim.toValue = [NSNumber numberWithInt:-20];
        [_personImageView.layer addAnimation:personAnim forKey:@"kPersonAnimation"];
        
        CABasicAnimation *wordAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
        wordAnim.delegate = self;
        wordAnim.duration = _animDuration;
        wordAnim.toValue = [NSNumber numberWithInt:0];
        wordAnim.removedOnCompletion = NO;
        wordAnim.fillMode = kCAFillModeForwards;
        [wordAnim setValue:@"wordAnim" forKey:@"animid"];
        [_wordImageView.layer addAnimation:wordAnim forKey:@"kWordAnimation"];
        
        CABasicAnimation *cloudAnim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        cloudAnim.duration = _animDuration * 2;
        cloudAnim.removedOnCompletion = NO;
        cloudAnim.fillMode = kCAFillModeForwards;
        cloudAnim.toValue = [NSNumber numberWithFloat:6.285];
        [_cloudImageView.layer addAnimation:cloudAnim forKey:@"kCloudAnimation"];
    }
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    NSString *animid = [anim valueForKey:@"animid"];
    if([animid isEqualToString:@"wordAnim"]) {
        CABasicAnimation *startAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
        startAnim.delegate = self;
        startAnim.duration = _animDuration;
        startAnim.toValue = [NSNumber numberWithInt:1];
        startAnim.removedOnCompletion = NO;
        startAnim.fillMode = kCAFillModeForwards;
        [startAnim setValue:@"startAnim" forKey:@"animid"];
        [_startButton.layer addAnimation:startAnim forKey:@"kStartButtonAnimation"];
    } else if([animid isEqualToString:@"startAnim"]) {
        _startButton.alpha = 1;
    } else if([animid isEqualToString:@"moveToLeft"]) {
        [self removeFromSuperview];
    }
}

- (void)startButtonClicked
{
    CABasicAnimation *currViewAnim = [CABasicAnimation animationWithKeyPath:@"position.x"];
    currViewAnim.delegate = self;
    currViewAnim.duration = 0.5;
    currViewAnim.toValue = [NSNumber numberWithInt:-SCREEN_WIDTH];
    currViewAnim.removedOnCompletion = NO;
    currViewAnim.fillMode = kCAFillModeForwards;
    [currViewAnim setValue:@"moveToLeft" forKey:@"animid"];
    [self.layer addAnimation:currViewAnim forKey:@"kMoveToLeft"];
}

@end
